/*******************群组修改***********************/
$(function(){
	/**
	 * 下拉区域
	 */
	$(".js_sel_1List .item").click(function(){
		var text = this.innerHTML;
		var val = $(this).attr("value");
		$(this).closest(".rolePer").find(".roleItem").val(val);
		$(".js_sel_1")[0].innerHTML=text;
		commmon_initRole();
	});
	$(".js_sel_2List .item").click(function(){
		var text = this.innerHTML;
		$(".js_sel_2")[0].innerHTML=text;
		var val = $(this).attr("value");
		$(this).closest(".rolePer").find(".roleItem").val(val);
		commmon_initRole();
	});
	$(".js_sel_3List .item").click(function(){
		var text = this.innerHTML;
		$(".js_sel_3")[0].innerHTML=text;
		var val = $(this).attr("value");
		$(this).closest(".rolePer").find(".roleItem").val(val);
		commmon_initRole();
	});
	
	$(".js_sel_4List .item").click(function(){
		var text = this.innerHTML;
		$(".js_sel_4")[0].innerHTML=text;
		var val = $(this).attr("value");
		$(this).closest(".rolePer").find(".roleItem").val(val);
		commmon_initRole();
	});
	
	$(".js_sel_5List .item").click(function(){
		var text = this.innerHTML;
		$(".js_sel_5")[0].innerHTML=text;
		var val = $(this).attr("value");
		$(this).closest(".rolePer").find(".roleItem").val(val);
	});
	
	$(".js_sel_6List .item").click(function(){
		var text = this.innerHTML;
		$(".js_sel_6")[0].innerHTML=text;
		var val = $(this).attr("value");
		$(this).closest(".rolePer").find(".roleItem").val(val);
	});
	
	$(".js_sel_7List .item").click(function(){
		var text = this.innerHTML;
		$(".js_sel_7")[0].innerHTML=text;
		var val = $(this).attr("value");
		$(this).closest(".rolePer").find(".roleItem").val(val);
	});
	//跳转到群组list
	$(".logo-img").click(function(){
		window.location.href="../meetingList/meetingList.html#"+queryOrgId;
	});
	//群组信息
	$.ajax({
		url:"../../../hivelearn/group/findGroup.json?id="+queryGroupId+"&groupId="+queryGroupId,
		success:function(datas){
			var group = datas.data;
			var groupName = group.groupName;
			$(".groupName").text(groupName);
			/*if(groupName.length>15){
				$(".groupName").text(groupName.substring(0,15)+"...");
			}else{
				$(".groupName").text(groupName);
			}*/
			$(".groupName").attr("title",groupName);
			var ctime = new Date(group.createDate);
			var year = ctime.getFullYear();
			var month = ctime.getMonth();
			//$(".groupCreateDate").text('（'+year + '年' + month+'月'+'）');
			
			if(group.groupCover){
				$(".groupImg").find("img").attr("src","../../../components/gtattachment/open/downloadAttachment.json?attachmentId="
					+group.groupCover);
			}
		}
	})
})
function updateGroup(){
	if($("#groupInfoModal").find(".groupUpdateImg").find("img")[0]){
		$("#groupInfoModal").find(".groupUpdateImg").find("img").remove();
	}
	$("#groupName").val("");
	$("#groupDes").val("");
	$("#groupInfoModal").find("[name='groupId']").val(queryGroupId);
	$.get('../../../hivelearn/group/findGroup.json?id='+queryGroupId+"&groupId="+queryGroupId, function(datas) {
		if (datas.success) {
			var group = datas.data;
			$("#groupName").val(group.groupName);
			$("#groupName").unbind("keyup");
			$("#groupName").keyup(function(){
				if($(this).val()){
					$(this).closest("#groupInfoModal").find(".error-msg").text("");
				}
			})
			$("#groupDes").val(group.aboutGroup);
			$("#groupInfoModal").find("[name='ORCHID_WEB_TOKEN']").val(datas.tokenmessage.token);
			$('[name="groupCover"]').val(group.groupCover);
			if(group.groupCover){
				var img = "<img id=\"topicCover\" src=\"../../../components/gtattachment/open/downloadAttachment.json?attachmentId="
					+group.groupCover+"\"/>";
				$("#groupInfoModal").find(".groupUpdateImg").append(img);
			}
			$("#groupInfoModal").modal({backdrop: 'static', keyboard: true});
		}
	})
}
//保存群组修改信息
function updateGroupInfo(){
	var gourpDes = $("#groupDes").val();
	$("[name='aboutGroup']").val(gourpDes);
	var groupName = $("#groupName").val();
	if(!groupName||!$.trim(groupName)){
		$("#updateGroupInfo").find(".error-msg").text("名称不能为空！")
		return;
	}
	$.ajax({
		url:"../../../hivelearn/group/updateGroup.json",
		data : $('#updateGroupInfo').serialize(),
		type : "POST",
		success:function(){
			//刷新
			location.reload();
		}
	})
}
function findGroupSetting(){
	getGroupRole(queryGroupId);
	//初始化角色资源（群组设置中展示用）
	$.ajax({
		url:"../../../hivelearn/group/findGroupSetting.json",
		data:{"id":queryGroupId,"groupId":queryGroupId},
		success:function(data){
			var d = data.data;
			$("#groupForm").find("[name='ORCHID_WEB_TOKEN']").val(data.tokenmessage.token);
			$("#groupForm").find("[name='groupId']").val(queryGroupId);
			for(var i = 0;i<d.length;i++){
				//默认角色
				if(d[i].isDefaultRole==1){
					$("[name='defaultRole']").each(function(){
						if($(this).val()==d[i].isDefaultRole){
							if(d[i].roleCode=="commenter"){
								$(this).attr("check",true);
							}else if(d[i].roleCode=="editor"){
								$(this).attr("check",true);
							}
						}
					});
				}
				var $ele= $("#groupForm").find("[name='"+d[i].roleCode+"']");
				if($ele[0]){
					var roleScope=d[i].roleScope;
					$ele.val(roleScope);
					if(roleScope==4){
						$ele.closest(".sortItem").find(".itemName").text("所有人");
					}else if(roleScope==3){
						$ele.closest(".sortItem").find(".itemName").text("编辑、管理员和自己");
					}else if(roleScope==2){
						$ele.closest(".sortItem").find(".itemName").text("管理员和自己");
					}else if(roleScope==1){
						$ele.closest(".sortItem").find(".itemName").text("自己");
					}
				}
			}
		}
	})
	$("#groupConfModal").modal({backdrop: 'static', keyboard: true});
}
function updateGroupSetting(){
	$.ajax({
		url:"../../../hivelearn/group/updateGroupSetting.json",
		method:"post",
		data:$("#groupForm").serialize(),
		success:function(data){
			
		}
	});
}
function aboutGroup(){
	$.ajax({
		url:"../../../hivelearn/group/findAboutGroup.json",
		data:{id:queryGroupId},
		success:function(data){
			if(data.data.aboutGroup){
				$(".aboutGroupInfo").text(data.data.aboutGroup);
			}
			var createDate = new Date(data.data.createDate);
			var year = createDate.getFullYear();
			var month = createDate.getMonth()<10?"0"+createDate.getMonth():createDate.getMonth();
			var day = createDate.getDate()<10?"0"+createDate.getDate():createDate.getDate();
			$(".GroupCreateInfo").empty();
			$(".GroupCreateInfo").append("该组由<span style='font-weight:bold;'>"+data.data.userName+"</span>创建于"+year+"年"+month+"月"+day+"日");
			var defaultRole = data.data.defaultCode=='editor'?"人员":"评论者";
			$(".GroupRoleInfo").empty();
			$(".GroupRoleInfo").append("组中默认角色为:<span style='font-weight:bold;'>"+defaultRole+"</span>");
			$(".currentRoleInfo").empty();
			var currentUserRole= data.data.currentUserRole;
			var currentRole = currentUserRole=='creater'?'创建者':(currentUserRole=='admin'?'管理者':(currentUserRole=='editor'?'人员':(currentUserRole=='viewer'?'观看者':'评论者')));
			$(".currentRoleInfo").append("我在当前组的角色为:<span style='font-weight:bold;'>"+currentRole+"</span>");
			$("#aboutGroupModal").find(".selectRoleItem").each(function(){
				var cls = $(this).find("div").attr("class");
				for(var i = 0;i<5;i++){
					var d = data.data.roles;
					if(cls=="editor"){
						$(this).find("div").text(changeShow(d.editor));
					}else if(cls=="commenter"){
						$(this).find("div").text(changeShow(d.commenter));
					}else if(cls=="viewer"){
						$(this).find("div").text(changeShow(d.viewer));
					}else if(cls=="admin"){
						$(this).find("div").text(changeShow(d.admin));
					}else if(cls=="creater"){
						$(this).find("div").text(changeShow(d.creater));
					}
				}
			})
		}
	})
	$("#aboutGroupModal").modal({backdrop: 'static', keyboard: true});
}
function delGroup(){
	$("#groupMsgModal").modal("show");
}
function deleteGroup(){
	$.ajax({
		url:"../../../hivelearn/group/deleteGroupById.json",
		data:{id:queryGroupId,groupId:queryGroupId},
		success:function(data){
			window.location.href="../meetingList/meetingList.html#"+queryOrgId;
		}
	})
}
/***********图片上传*************/
function uploadGroupFile() {
	$("#uploadGroupForm").ajaxSubmit({
    	url: "../../../hivelearn/topic/uploadImage.json?_t="+new Date().getTime(),
        data:$("#uploadGroupForm").serialize(),
        dataType:"json",
        success:function(data){
        	if($("#groupInfoModal").find(".groupUpdateImg").find("img")[0]){
				$("#groupInfoModal").find(".groupUpdateImg").find("img").attr("src","../../../components/gtattachment/open/downloadAttachment.json?attachmentId="
						+ data.attachid);
			}else{
				var img = "<img src=\"../../../components/gtattachment/open/downloadAttachment.json?attachmentId="
					+data.attachid+"\"/>";
				$("#groupInfoModal").find(".groupUpdateImg").append(img);
			}
			$('[name="groupCover"]').val(data.attachid);
			console.log("上传成功");
			$("#groupfile").val("");
		},
		error:function(data){
			console.log("服务器异常");
			$("#groupfile").val("");
		}
    });
}
//检查
/*function checkfile(target) {
	var isIE = /msie/i.test(navigator.userAgent) && !window.opera; 
	var fileSize = 0;
	var filetypes = [ ".jpg", ".png", ".gif"];
	var filepath = target.value;
	var filemaxsize = 1024 * 2;// 2M
	if (filepath) {
		var isnext = false;
		var fileend = filepath.substring(filepath.indexOf("."));
		if (filetypes && filetypes.length > 0) {
			for ( var i = 0; i < filetypes.length; i++) {
				if (filetypes[i] == fileend) {
					isnext = true;
					break;
				}
			}
		}
		if (!isnext) {
			$("#msgModal").find(".msg").text("请选择.png，.jpg，.gif图片文件上传");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
	} else {
		return false;
	}
	if (isIE && !target.files) {
		ieUpload(target,"uploadGroupForm",uploadGroupFile,filemaxsize);
	} else {
		fileSize = target.files[0].size;
		var size = fileSize / 1024;
		if (size > filemaxsize) {
			$("#msgModal").find(".msg").text("图片大小不能大于" + filemaxsize / 1024 + "M！");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
		if (size <= 0) {
			$("#msgModal").find(".msg").text("图片大小不能为0M！");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
		uploadGroupFile();
	}
}*/
/***********图片上传*************/
/*******************群组修改***********************/

